package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Entity.Apply;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TeaApply_detailsActivity extends AppCompatActivity {

    private String TAG = "TeaApply_detailsActivity测试";

    private TextView tea_apply_details_WorkerName;
    private TextView tea_apply_details_WorkerSex;
    private TextView tea_apply_details_WorkerAge;
    private TextView tea_apply_details_Workercarid;
    private TextView tea_apply_details_WorkerPhone;
    private TextView tea_apply_details_WorkerEmail;
    private TextView tea_apply_details_WorkerId;
    private TextView tea_apply_details_WorkerbackTime;
    private TextView tea_apply_details_WorkerLocal;
    private Button tea_apply_details_btn1;
    private Button tea_apply_details_btn2;

    private LinearLayout tea_apply_details_operationlinear;
    private String ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tea_apply_details);
        initid();
        Intent intent = getIntent();
        Apply apply = (Apply) intent.getSerializableExtra("apply");
        ids = intent.getStringExtra("id");
        find(apply);
        if (ids.equals("3")){
            tea_apply_details_operationlinear.setVisibility(View.GONE);
        }
        tea_apply_details_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                depttj(apply,1);
            }
        });
        tea_apply_details_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                depttj(apply,2);
            }
        });
    }

    private void initid(){
        tea_apply_details_WorkerId = (TextView) findViewById(R.id.tea_apply_details_WorkerId);
        tea_apply_details_WorkerName = (TextView) findViewById(R.id.tea_apply_details_WorkerName);
        tea_apply_details_WorkerSex = (TextView) findViewById(R.id.tea_apply_details_WorkerSex);
        tea_apply_details_WorkerAge = (TextView) findViewById(R.id.tea_apply_details_WorkerAge);
        tea_apply_details_Workercarid = (TextView) findViewById(R.id.tea_apply_details_Workercarid);
        tea_apply_details_WorkerPhone = (TextView) findViewById(R.id.tea_apply_details_WorkerPhone);
        tea_apply_details_WorkerEmail = (TextView) findViewById(R.id.tea_apply_details_WorkerEmail);
        tea_apply_details_WorkerLocal = (TextView) findViewById(R.id.tea_apply_details_WorkerLocal);
        tea_apply_details_WorkerbackTime = (TextView) findViewById(R.id.tea_apply_details_WorkerbackTime);
        tea_apply_details_btn1 = (Button) findViewById(R.id.tea_apply_details_btn1);
        tea_apply_details_btn2 = (Button) findViewById(R.id.tea_apply_details_btn2);

        tea_apply_details_operationlinear = (LinearLayout) findViewById(R.id.tea_apply_details_operationlinear);
    }

    private void find(Apply apply){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Worker/GetWorder?workerId="+apply.getApplicantsId())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(apply.getInfoJson());
                    JSONObject jsonObject1 = new JSONObject(message);
                    JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                    Log.d("jsonObject1",jsonObject1.toString());
                    Log.d("jsonObject2",jsonObject2.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                tea_apply_details_WorkerId.setText(apply.getApplicantsId()+"的申请表");
                                tea_apply_details_WorkerName.setText(jsonObject2.getString("WorkerName"));
                                tea_apply_details_WorkerSex.setText(jsonObject2.getString("WorkerSex"));
                                tea_apply_details_WorkerAge.setText(jsonObject2.getString("WorkerAge"));
                                tea_apply_details_Workercarid.setText(jsonObject2.getString("WorkerCarid"));
                                tea_apply_details_WorkerPhone.setText(jsonObject2.getString("WorkerPhone"));
                                tea_apply_details_WorkerEmail.setText(jsonObject2.getString("WorkerEmail"));
                                tea_apply_details_WorkerLocal.setText(jsonObject2.getString("WorkerLocal"));

                                String times = jsonObject2.getString("WorkerbackTime");
                                if (times.length()>=10){
                                    times = times.substring(0,10);
                                }
                                tea_apply_details_WorkerbackTime.setText(times);
                                if (jsonObject1.getString("WorkerName").equals(jsonObject2.getString("WorkerName"))){
                                    tea_apply_details_WorkerName.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerName.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerSex").equals(jsonObject2.getString("WorkerSex"))){
                                    tea_apply_details_WorkerSex.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerSex.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerAge").equals(jsonObject2.getString("WorkerAge"))){
                                    tea_apply_details_WorkerAge.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerAge.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerCarid").equals(jsonObject2.getString("WorkerCarid"))){
                                    tea_apply_details_Workercarid.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_Workercarid.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerPhone").equals(jsonObject2.getString("WorkerPhone"))){
                                    tea_apply_details_WorkerPhone.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerPhone.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerEmail").equals(jsonObject2.getString("WorkerEmail"))){
                                    tea_apply_details_WorkerEmail.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerEmail.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerLocal").equals(jsonObject2.getString("WorkerLocal"))){
                                    tea_apply_details_WorkerLocal.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerLocal.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("WorkerbackTime").equals(times)){
                                    tea_apply_details_WorkerbackTime.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    tea_apply_details_WorkerbackTime.setTextColor(Color.parseColor("#ff0000"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void depttj(Apply apply,int status){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("Id",apply.getId());
                    jsonObject1.put("ApplicantsId",apply.getApplicantsId());
                    jsonObject1.put("InfoJson",apply.getInfoJson());
                    jsonObject1.put("ByTheTime",apply.getByTheTime());
                    jsonObject1.put("ApplicationType",apply.getApplicationType());
                    jsonObject1.put("Status",apply.getStatus());

                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    RequestBody requestBody = RequestBody.create(mediaType,jsonObject1.toString()+"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/TheApplicationForm/AlterStuDeptApproval?token="+Sto.dept.getToken()+"&status="+status)
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    Log.d(TAG+"msg",msg);
                    JSONArray jsonArray = new JSONArray(msg);
                    if (jsonArray.length()==1){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(TeaApply_detailsActivity.this,"拒绝成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }else {
                        JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                        String Id1 = jsonObject2.getString("Id");
                        String message1 = jsonObject2.getString("message");
                        JSONObject jsonObject22 = new JSONObject(message1);
                        int message11 = jsonObject22.getInt("message");
                        JSONObject jsonObject3 = jsonArray.getJSONObject(1);
                        String Id2 = jsonObject3.getString("Id");
                        String message2 = jsonObject3.getString("message");
                        JSONArray jsonArray1 = new JSONArray(message2);
                        JSONObject jsonObject33 = jsonArray1.getJSONObject(0);
                        String message22 = jsonObject33.getString("message");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String result = "";
                                if (message11>0){
                                    result += Id1+"成功，";
                                }else {
                                    result += Id1+"失败，";
                                }
                                if (message22.equals("error")){
                                    result = result+Id2+"失败";
                                }else if (message22.equals("success")){
                                    result = result+Id2+"成功";
                                }else {
                                    result = result+Id2+message22;
                                }
                                Toast.makeText(TeaApply_detailsActivity.this,result,Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}